﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using University.Workshop.Model;
using University.Workshop.DataAccess;

namespace University.Workshop.ClientApp
{
    /// <summary>
    /// Lógica de interacción para LuckWithoutWhite.xaml
    /// </summary>
    public partial class LuckWithoutWhite : Window
    {
        int counter1 = 0, counter2 = 0, counter3 = 0;
        Client client = new Client();
        User user = new User();
        long amount = 0;
        bool Evaluate = true;
        public bool play = false, finish = false,Banned=false;
        public LuckWithoutWhite()
        {
            InitializeComponent();
            
        }
        public LuckWithoutWhite(User user)
        {
            InitializeComponent();
            this.user = user;
        }
        public int Randomize()
        {
            if(Evaluate && !client.GetGameStatus(user,"Jocker"))
            {
                MessageBox.Show("you have already played today, try again tomorrow");
                this.Close();
            }
            Evaluate = false;
            int number = 0;
            play = true;
            Random random = new Random();
            bool done = false;
            while (!done)
            {
                number = random.Next(1, 4);
                if ((number == 1) && (counter1 < 4))
                {
                    counter1++;
                    done = true;
                }
                else
                {
                    if ((number == 2) && (counter2 < 4))
                    {
                        counter2++;
                        done = true;
                    }
                    else
                    {
                        if ((number == 3) && (counter3 < 2))
                        {
                            counter3++;
                            done = true;
                        }
                    }
                }
            }
            return number;
        }
        public bool saveChips(long money)
        {
            client.SetGameStatus(user, "Jocker");
            if (money == 0)
            {
                MessageBox.Show("Sorry you didn't won chips, try again tomorrow");
                return false;
            }
            else
            {
                if (money < 0)
                {
                    MessageBox.Show("Sorry you lost, try again tomorrow");
                    return false;
                }
                else
                {
                    MessageBox.Show("Congratulations you won!!");
                    user.Chips = user.Chips + amount;
                    client.Send("14 " + user.Name + " " + user.Chips);
                    client.Receive();

                    return true;
                }
            }
        }


        public bool Quit()
        {
            if ((play) && (!finish))
            {
                MessageBoxResult quitMessage = MessageBox.Show("Are you sure you wanna quit?", "Warning!!!", MessageBoxButton.YesNo);
                if (quitMessage == MessageBoxResult.Yes)
                {
                    saveChips(Convert.ToInt32(chips.Content));
                    return true;
                }
                else
                    return false;
            }
            else
                return true;
        }

        private void Leave_Click(object sender, RoutedEventArgs e)
        {
            //Quit();
            Close();
        }
        public bool end(int index)
        {
            switch (index)
            {
                case 1:
                    amount = amount + 1000;
                    break;
                case 2:
                    amount = amount - 1000;
                    break;
                case 3:
                    amount = amount + 500;
                    break;
            }
            chips.Content = Convert.ToString(amount);
            if (counter1 == 4)
            {
                amount = amount + 2000;
                chips.Content = Convert.ToString(amount);
                saveChips(amount);
                finish = true;
                return true;
            }
            else
            {
                if ((counter2 - counter3) == 2)
                {
                    amount = -1;
                    saveChips(amount);
                    finish = true;
                    return true;
                }
                else
                    return false;
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (!Banned)
            {
                if (!Quit())
                    e.Cancel = true;
            }
        }

        private void image1_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            int index = Randomize();
            if (index == 1)
                image1.Source = (ImageSource)new ImageSourceConverter().ConvertFrom(new Uri(@"pack://application:,,,/University.Workshop.ClientApp;component/Images/uno.png"));
            else
            {
                if (index == 2)
                    image1.Source = (ImageSource)new ImageSourceConverter().ConvertFrom(new Uri(@"pack://application:,,,/University.Workshop.ClientApp;component/Images/joker.jpg"));
                else
                    image1.Source = (ImageSource)new ImageSourceConverter().ConvertFrom(new Uri(@"pack://application:,,,/University.Workshop.ClientApp;component/Images/trece.png"));
            }
            image1.IsEnabled = false;
            if (end(index))
                Close();
        }

        private void image2_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            int index = Randomize();
            if (index == 1)
                image2.Source = (ImageSource)new ImageSourceConverter().ConvertFrom(new Uri(@"pack://application:,,,/University.Workshop.ClientApp;component/Images/uno.png"));
            else
            {
                if (index == 2)
                    image2.Source = (ImageSource)new ImageSourceConverter().ConvertFrom(new Uri(@"pack://application:,,,/University.Workshop.ClientApp;component/Images/joker.jpg"));
                else
                    image2.Source = (ImageSource)new ImageSourceConverter().ConvertFrom(new Uri(@"pack://application:,,,/University.Workshop.ClientApp;component/Images/trece.png"));
            }
            image2.IsEnabled = false;
            if (end(index))
                Close();
        }

        private void image3_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            int index = Randomize();
            if (index == 1)
                image3.Source = (ImageSource)new ImageSourceConverter().ConvertFrom(new Uri(@"pack://application:,,,/University.Workshop.ClientApp;component/Images/uno.png"));
            else
            {
                if (index == 2)
                    image3.Source = (ImageSource)new ImageSourceConverter().ConvertFrom(new Uri(@"pack://application:,,,/University.Workshop.ClientApp;component/Images/joker.jpg"));
                else
                    image3.Source = (ImageSource)new ImageSourceConverter().ConvertFrom(new Uri(@"pack://application:,,,/University.Workshop.ClientApp;component/Images/trece.png"));
            }
            image3.IsEnabled = false;
            if (end(index))
                Close();
        }

        private void image4_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            int index = Randomize();
            if (index == 1)
                image4.Source = (ImageSource)new ImageSourceConverter().ConvertFrom(new Uri(@"pack://application:,,,/University.Workshop.ClientApp;component/Images/uno.png"));
            else
            {
                if (index == 2)
                    image4.Source = (ImageSource)new ImageSourceConverter().ConvertFrom(new Uri(@"pack://application:,,,/University.Workshop.ClientApp;component/Images/joker.jpg"));
                else
                    image4.Source = (ImageSource)new ImageSourceConverter().ConvertFrom(new Uri(@"pack://application:,,,/University.Workshop.ClientApp;component/Images/trece.png"));
            }
            image4.IsEnabled = false;
            if (end(index))
                Close();
        }

        private void image5_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            int index = Randomize();
            if (index == 1)
                image5.Source = (ImageSource)new ImageSourceConverter().ConvertFrom(new Uri(@"pack://application:,,,/University.Workshop.ClientApp;component/Images/uno.png"));
            else
            {
                if (index == 2)
                    image5.Source = (ImageSource)new ImageSourceConverter().ConvertFrom(new Uri(@"pack://application:,,,/University.Workshop.ClientApp;component/Images/joker.jpg"));
                else
                    image5.Source = (ImageSource)new ImageSourceConverter().ConvertFrom(new Uri(@"pack://application:,,,/University.Workshop.ClientApp;component/Images/trece.png"));
            }
            image5.IsEnabled = false;
            if (end(index))
                Close();
        }

        private void image6_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            int index = Randomize();
            if (index == 1)
                image6.Source = (ImageSource)new ImageSourceConverter().ConvertFrom(new Uri(@"pack://application:,,,/University.Workshop.ClientApp;component/Images/uno.png"));
            else
            {
                if (index == 2)
                    image6.Source = (ImageSource)new ImageSourceConverter().ConvertFrom(new Uri(@"pack://application:,,,/University.Workshop.ClientApp;component/Images/joker.jpg"));
                else
                    image6.Source = (ImageSource)new ImageSourceConverter().ConvertFrom(new Uri(@"pack://application:,,,/University.Workshop.ClientApp;component/Images/trece.png"));
            }
            image6.IsEnabled = false;
            if (end(index))
                Close();
        }

        private void image7_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            int index = Randomize();
            if (index == 1)
                image7.Source = (ImageSource)new ImageSourceConverter().ConvertFrom(new Uri(@"pack://application:,,,/University.Workshop.ClientApp;component/Images/uno.png"));
            else
            {
                if (index == 2)
                    image7.Source = (ImageSource)new ImageSourceConverter().ConvertFrom(new Uri(@"pack://application:,,,/University.Workshop.ClientApp;component/Images/joker.jpg"));
                else
                    image7.Source = (ImageSource)new ImageSourceConverter().ConvertFrom(new Uri(@"pack://application:,,,/University.Workshop.ClientApp;component/Images/trece.png"));
            }
            image7.IsEnabled = false;
            if (end(index))
                Close();
        }

        private void image8_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            int index = Randomize();
            if (index == 1)
                image8.Source = (ImageSource)new ImageSourceConverter().ConvertFrom(new Uri(@"pack://application:,,,/University.Workshop.ClientApp;component/Images/uno.png"));
            else
            {
                if (index == 2)
                    image8.Source = (ImageSource)new ImageSourceConverter().ConvertFrom(new Uri(@"pack://application:,,,/University.Workshop.ClientApp;component/Images/joker.jpg"));
                else
                    image8.Source = (ImageSource)new ImageSourceConverter().ConvertFrom(new Uri(@"pack://application:,,,/University.Workshop.ClientApp;component/Images/trece.png"));
            }
            image8.IsEnabled = false;
            if (end(index))
                Close();
        }

        private void image9_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            int index = Randomize();
            if (index == 1)
                image9.Source = (ImageSource)new ImageSourceConverter().ConvertFrom(new Uri(@"pack://application:,,,/University.Workshop.ClientApp;component/Images/uno.png"));
            else
            {
                if (index == 2)
                    image9.Source = (ImageSource)new ImageSourceConverter().ConvertFrom(new Uri(@"pack://application:,,,/University.Workshop.ClientApp;component/Images/joker.jpg"));
                else
                    image9.Source = (ImageSource)new ImageSourceConverter().ConvertFrom(new Uri(@"pack://application:,,,/University.Workshop.ClientApp;component/Images/trece.png"));
            }
            image9.IsEnabled = false;
            if (end(index))
                Close();
        }

        private void image10_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            int index = Randomize();
            if (index == 1)
                image10.Source = (ImageSource)new ImageSourceConverter().ConvertFrom(new Uri(@"pack://application:,,,/University.Workshop.ClientApp;component/Images/uno.png"));
            else
            {
                if (index == 2)
                    image10.Source = (ImageSource)new ImageSourceConverter().ConvertFrom(new Uri(@"pack://application:,,,/University.Workshop.ClientApp;component/Images/joker.jpg"));
                else
                    image10.Source = (ImageSource)new ImageSourceConverter().ConvertFrom(new Uri(@"pack://application:,,,/University.Workshop.ClientApp;component/Images/trece.png"));
            }
            image10.IsEnabled = false;
            if (end(index))
                Close();
        }

        public void Ban()
        {
            Banned = true;
            Close();
        }
    }
}